// pages/login/login.js
import { post, get } from '../../utils/request';
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    errPaw: false,
    username: '',
    password: '',
    role: '0',
    mail: null,
    mailVerificationCode: null,
    phone: null,
    gender: '男',
    jssessionid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // const username = wx.getStorageSync('username')
    // const password = wx.getStorageSync('password')
    // if (username && password) {
    //   this.setData({
    //     username: username,
    //     password: password,
    //   })
    //   this.login()
    // }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  handleChangeRole(e) {
    console.log(e);
    this.setData({
      role: e.detail.value,
    });
  },

  handleChangeGender(e) {
    console.log(e);
    this.setData({
      gender: e.detail.value,
    });
  },

  changeMail(e) {
    console.log(e);
    this.setData({
      mail: e.detail.value,
    });
  },

  getMailCode() {
    wx.request({
      url: `http://8.130.99.108:8090/api/sendEmailCode`, //仅为示例，并非真实的接口地址
      data: {
        email: this.data.mail,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      method: 'GET',
      success: (res) => {
        var cookies = res.header['Set-Cookie'];
        // 提取 jssessionid
        var jssessionid = cookies.match(/JSESSIONID=(\S*);/)[1];

        this.setData({
          jssessionid,
        });
        if (res.data.message == '服务运行成功') {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '验证码已经发送请注意查收',
          });
        } else {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '服务器出错啦,请稍后再试',
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },

  formSubmit(e) {
    // console.log('form发生了submit事件，携带数据为：', e.detail.value);

    const { username, password, mail, phone, mailVerificationCode } = e.detail.value;
    const { role, gender, jssessionid } = this.data;

    wx.request({
      url: `http://8.130.99.108:8090/api/signUp`, //仅为示例，并非真实的接口地址
      data: {
        username,
        password,
        role: parseInt(role),
        email: mail,
        mailVerificationCode: parseInt(mailVerificationCode),
        gender,
        phoneNumber: phone,
      },
      header: {
        'content-type': 'application/json', // 默认值
        cookie: 'JSESSIONID=' + jssessionid,
      },
      method: 'POST',
      success: (res) => {
        if (res.data.message == '服务运行成功') {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '注册成功',
          });
          // if (role == '0') {
          //   wx.switchTab({
          //     url: `/pages/goods/category/index`,
          //     success: (result) => {},
          //     fail: () => {},
          //     complete: () => {},
          //   });
          // } else {
          //   wx.switchTab({
          //     url: `/pages/scanCode/scanCode`,
          //     success: (result) => {
          //       console.log(result);
          //     },
          //     fail: (e) => {
          //       console.log(e);
          //     },
          //     complete: (e) => {
          //       console.log(e);
          //     },
          //   });
          // }
          wx.navigateTo({
            url: '/pages/login/login',
          });
        } else {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '注册失败,请重新填写完整信息',
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });
    // post('/signUp', {
    //   username,
    //   password,
    //   role: parseInt(role),
    //   email: mail,
    //   mailVerificationCode,
    //   gender,
    //   phoneNumber: phone,
    // }).then((res) => {
    //   if (res.message == '服务运行成功') {
    //     Toast({
    //       context: this,
    //       selector: '#t-toast',
    //       message: '注册成功',
    //     });
    //     if (role == '0') {
    //       wx.switchTab({
    //         url: `/pages/goods/category/index`,
    //         success: (result) => {},
    //         fail: () => {},
    //         complete: () => {},
    //       });
    //     } else {
    //       wx.switchTab({
    //         url: `/pages/scanCode/scanCode`,
    //         success: (result) => {
    //           console.log(result);
    //         },
    //         fail: (e) => {
    //           console.log(e);
    //         },
    //         complete: (e) => {
    //           console.log(e);
    //         },
    //       });
    //     }
    //   } else {
    //     Toast({
    //       context: this,
    //       selector: '#t-toast',
    //       message: '注册失败,请重新填写完整信息',
    //     });
    //   }
    // });
  },
});
